oo::class create Variable {
    superclass Plugin
    variable RenamedPadWidget Language

    constructor {renamedPadWidget language} {
        set RenamedPadWidget $renamedPadWidget
        set Language $language
    }

    method hilightVariable {from to} {
        set from [$Language getWordHeadIndex $RenamedPadWidget $from]
        if [$RenamedPadWidget compare $from != "$from linestart"] {
            set from [$RenamedPadWidget index "$from -1c"]
        }
        set to [$Language getWordEndIndex $RenamedPadWidget $to]
        $RenamedPadWidget tag remove Variable $from $to
        $RenamedPadWidget tag remove Proc $from $to
        
        set content [$RenamedPadWidget get $from $to]
        set prevContent [$RenamedPadWidget get "$from linestart" $from]
        set prevWord [lindex [split [string trimright $prevContent]] end]
        set ph [string last $prevWord $prevContent]
        set prevIds [list $ph [expr $ph+[string length $prevWord]]]
        
        set VariableIds {}
        set ProcIds {}
        
        foreach ids [regexp -inline -indices -all [$Language getVariableRelatedRegexp] $content] {
            foreach {h t} $ids {}
            set curWord [string range $content $h $t]
            if [$Language isVariable? [string range $content $h-1 $t]] {
                lappend VariableIds [list "$from +${h}c" "$from +${t}c +1c"]
                # $RenamedPadWidget tag add Variable "$from +${h}c" "$from +${t}c +1c"
            } else {
                foreach type {Variable Proc} {
                    # set wt [expr {$type == "Variable" ? "vtags" : "ptags"}]
                    if [set which [$Language is${type}Definition? $prevWord $curWord]] {
                        if {$which == 1} {
                            lappend ${type}Ids [list "$from +${h}c" "$from +${t}c +1c"]
                            # $RenamedPadWidget tag add $type "$from +${h}c" "$from +${t}c +1c"
                        } else {
                            # which == -1 or else
                            foreach {ph pt} $prevIds {}
                            lappend ${type}Ids [list "$from +${ph}c" "$from +${pt}c +1c"]
                            # $RenamedPadWidget tag add $type "$from +${ph}c" "$from + ${$pt}c"
                        }
                    }
                }
            }
            set prevWord $curWord
            set prevIds $ids
        }
        
        # NEW
        foreach t [list Variable Proc] {
            foreach ids [set ${t}Ids] {
                $RenamedPadWidget tag add $t {*}$ids
            }
        }
    }

    method hilightVariableAfterInsert {from content args} {
        my hilightVariable $from [$RenamedPadWidget index "$from +[string length $content]c"]
    }

    method hilightVariableAfterDelete {index args} {
        my hilightVariable $index $index
    }

    method hilightVariableAfterReplace {from to content args} {
        my hilightVariable $from [$RenamedPadWidget index "$from +[string length $content]c"]
    }

    method getPluginEvents {} {
        return {
            {afterInsert hilightVariableAfterInsert}
            {afterDelete hilightVariableAfterDelete}
            {afterReplace hilightVariableAfterReplace}
        }
    }
}